import numpy as np

from PIL import Image, ImageDraw, ImageFont


def correct_roi(roi):
    # clock wise angle
    center = roi[0]
    (w, h) = roi[1]
    min_rect_angle = roi[2]
    if h > w:
        min_rect_angle = min_rect_angle - 90
        roi = ((center[0], center[1]), (h, w), min_rect_angle)
    else:
        roi = ((center[0], center[1]), (w, h), min_rect_angle)
    return roi


def putTextPIL(im, str, loc, font, size, color, *args):
    im = Image.fromarray(im)
    ImageDraw.Draw(im).text(loc, str, color, font=ImageFont.truetype("simhei.ttf", int(30 * size)))
    im = np.array(im)
    return im
